package lm.concurrency.day2.eightlocks;

import lm.concurrency.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: Min.Liu
 * @Description: 线程8锁
 * @Date: Created in 11:21 2024/11/5
 */
public class Demo {
    public static void main(String[] args) {
        test7();
    }

    private static void test8() { //1s 后12， 或 2 1s后 1
        Number5 n1 = new Number5();
        Number5 n2 = new Number5();
        new Thread(() -> {
            n1.a();
        }).start();
        new Thread(() -> {
            n2.b();
        }).start();
    }

    private static void test7() { //2 1s 后 1
        Number4 n1 = new Number4();
        Number4 n2 = new Number4();
        new Thread(() -> {
            n1.a();
        }).start();//不互斥
        new Thread(() -> {
            n2.b();
        }).start();//不互斥
    }

    private static void test6() { //1s 后12， 或 2 1s后 1
        Number5 n1 = new Number5();
        new Thread(() -> {
            n1.a();
        }).start();
        new Thread(() -> {
            n1.b();
        }).start();
    }

    private static void test5() { //2 1s 后 1
        Number4 n1 = new Number4();
        new Thread(() -> {
            n1.a();
        }).start();//不互斥
        new Thread(() -> {
            n1.b();
        }).start();//不互斥
    }

    private static void test4() { //2 1s 后 1
        Number1 n1 = new Number1();
        Number1 n2 = new Number1();
        new Thread(() -> {
            n1.a();
        }).start();//不互斥
        new Thread(() -> {
            n2.b();
        }).start();//不互斥
    }

    private static void test3() { //3 1s 12 或 23 1s 1 或 32 1s 1

        //2 一秒后 1  1秒后1 2
        Number3 n1 = new Number3();
//        public synchronized void a() {
//            Sleeper.sleep(1);
//            log.debug("1");
//        }
//
//        public synchronized void b() {
//            log.debug("2");
//        }
//
//        public void c() {
//            log.debug("3");
//        }
        new Thread(() -> {
            n1.a();
        }).start();
        new Thread(() -> {
            n1.b();
        }).start();
        new Thread(() -> {
            n1.c();
        }).start(); //不互斥
    }

    private static void test2() { //1s后12，或 2 1s后 1
        Number2 n1 = new Number2();
        new Thread(() -> {
            n1.a();
        }).start();
        new Thread(() -> {
            n1.b();
        }).start();
    }

    private static void test1() { //12 或 21
        Number1 n1 = new Number1();
        new Thread(() -> {
            n1.a();
        }).start();
        new Thread(() -> {
            n1.b();
        }).start();
    }

}


@Slf4j
class Number1 {
    public synchronized void a() {
        log.debug("1");
    }

    public synchronized void b() {
        log.debug("2");
    }
}

@Slf4j
class Number2 {
    public synchronized void a() {
        Sleeper.sleep(1);
        log.debug("1");
    }

    public synchronized void b() {
        log.debug("2");
    }
}

@Slf4j
class Number3 {
    public synchronized void a() {
        Sleeper.sleep(1);
        log.debug("1");
    }

    public synchronized void b() {
        log.debug("2");
    }

    public void c() {
        log.debug("3");
    }
}

@Slf4j
class Number4 {
    public static synchronized void a() {
        Sleeper.sleep(1);
        log.debug("1");
    }

    public synchronized void b() {
        log.debug("2");
    }
}

@Slf4j
class Number5 {
    public static synchronized void a() {
        Sleeper.sleep(1);
        log.debug("1");
    }

    public static synchronized void b() {
        log.debug("2");
    }
}



